package com.china08.yxyapi.api.manage.tec.teaching.lesson.cloud;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonChapter;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLesson;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLessonItem;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonChapterRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonItemRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class TecCloudLessonToXiaoBenLessonController extends YxyApiBaseController {

    @Autowired
    YxyLessonsCloudLessonRepository lessonRepository;

    @Autowired
    YxyLessonsSchoolLessonRepository schoolLessonRepository;

    @Autowired
    YxyLessonsCloudLessonItemRepository itemRepository;

    @Autowired
    YxyLessonsVideoRepository videoRepository;

    @Autowired
    YxyLessonsSchoolLessonChapterRepository chapterRepository;

    @Autowired
    YxyLessonsSchoolLessonItemRepository schoolItemRepository;

    @ApiOperation("修改视频排序")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lesson/to/xiaoben/lesson", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) throws Exception {
        String lessonId = reqModel.lessonId;
        String catalogId1 = reqModel.catalog1;
        String catalogId2 = reqModel.catalog2;
        YxyProfile yxyProfile = getProfile(request, response);
        String schoolId = yxyProfile.getSchoolId();
        String author = yxyProfile.getUserNick();
        String authorId = yxyProfile.getId();

        // 获取区域课程
        YxyLessonsCloudLesson lesson = lessonRepository.findById(lessonId);
        if (null != lesson) {

            Integer isToSchoolLesson = lesson.getIsToSchoolLesson();
            Assert.isTrue(isToSchoolLesson == null || isToSchoolLesson != 1, "当前课程已经转入校本课程");

            // 总课时数
            Integer count = itemRepository.countByLessonIdAndStatusNot(lessonId, -1);

            // 总时长
            Integer duration = 0;
            List<YxyLessonsCloudLessonItem> itemList = itemRepository.findByLessonIdAndStatusNot(lessonId, -1);
            for (YxyLessonsCloudLessonItem item : itemList) {
                String videoId = item.getVideoId();
                if (StringUtils.isNotBlank(videoId)) {
                    YxyLessonsVideo video = videoRepository.findById(videoId);
                    if (null != video) {
                        duration = duration + video.getDuration();
                    }
                }
            }

            // 排序
            Integer orderNum = 1;
            YxyLessonsSchoolLesson schoolLesson = schoolLessonRepository.findTop1BySchoolIdOrderByOrderNumDesc(schoolId);
            if (schoolLesson != null) {
                orderNum = orderNum + schoolLesson.getOrderNum();
            }

            String name = lesson.getName();
            String summary = lesson.getSummary();
            schoolLesson = new YxyLessonsSchoolLesson();
            schoolLesson.setCatalogId1(catalogId1);
            schoolLesson.setCatalogId2(catalogId2);
            schoolLesson.setName(name);
            schoolLesson.setSummary(summary);
            schoolLesson.setCover(lesson.getCover());
            schoolLesson.setVip(0);
            schoolLesson.setCount(count);
            schoolLesson.setDuration(duration);
            schoolLesson.setCreateDate(new Date());
            schoolLesson.setStatus(1);
            schoolLesson.setPv(0);
            schoolLesson.setVv(0);
            schoolLesson.setIsVisible(0);
            schoolLesson.setSchoolId(schoolId);
            schoolLesson.setAuthor(author);
            schoolLesson.setAuthorId(authorId);
            schoolLesson.setOrderNum(orderNum);

            // 创建校本课程
            schoolLesson = schoolLessonRepository.saveAndFlush(schoolLesson);

            String schoolLessonId = schoolLesson.getId();
            // 创建校本课程章节
            YxyLessonsSchoolLessonChapter chapter = new YxyLessonsSchoolLessonChapter();
            chapter.setLessonId(schoolLessonId);
            chapter.setTitle(name);
            chapter.setIntro(summary);
            chapter.setOrderNum(1);
            chapter = chapterRepository.saveAndFlush(chapter);

            String chapterId = chapter.getId();
            Integer i = 1;
            // 创建课时
            for (YxyLessonsCloudLessonItem item : itemList) {
                String itemName = item.getTitle();
                String intro = item.getIntro();
                String videoId = item.getVideoId();

                YxyLessonsSchoolLessonItem it = new YxyLessonsSchoolLessonItem();
                it.setLessonId(schoolLessonId);
                it.setChapterId(chapterId);
                it.setTitle(itemName);
                it.setIntro(intro);
                it.setStatus(1);
                it.setVideoId(videoId);
                it.setOrderNum(i);
                schoolItemRepository.saveAndFlush(it);
                i++;
            }

            // 区域课程已经转入校本课程
            lesson.setIsToSchoolLesson(1);
            lessonRepository.saveAndFlush(lesson);
        }
    }

    public static class ReqModel extends YxyApiReqModel {
        public String lessonId;
        public String catalog1;
        public String catalog2;
    }
}
